package com.ynu.controller;

import com.ynu.bean.PageBean;
import com.ynu.bean.entity.Goods;
import com.ynu.bean.query.GoodsQuery;
import com.ynu.bean.vo.CategoryVO;
import com.ynu.bean.vo.GoodsVO;
import com.ynu.constant.WeGo;
import com.ynu.service.CategoryService;
import com.ynu.service.GoodsService;
import com.ynu.service.impl.GoodsServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 *  模块名称：
 *  模块类型：
 *  编码人：迟宽席
 *  创建时间：2023/3/1
 *  联系电话：18388068273
 */

/**
 * (tb_goods)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private CategoryService categoryService;

    @Resource
    private GoodsService goodsService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("openDetails")
    public ModelAndView openDetails(ModelAndView mav,Long id) {
        mav.setViewName("frontend/goods_details");
        Goods goods = goodsService.getByPrimaryKey(id);
        mav.addObject("goods",goods);

        //查找到需要显示的顶级类别
        List<CategoryVO> topCategoryVOList = categoryService.getTopCategory(WeGo.TOP_CATEGORY_AMOUNT);
        for (CategoryVO topCategoryVO : topCategoryVOList) {
            List<CategoryVO> secondCategoryVOList = categoryService.getCategoryByPid(topCategoryVO.getId(), 1,WeGo.SECOND_CATEGORY_AMOUNT);
            topCategoryVO.setSub(secondCategoryVOList);
        }
        mav.addObject("categoryList", topCategoryVOList);

        return mav;
    }

    /**
     * 指定父类别下的更多
     * @param mav
     * @param categoryId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public ModelAndView list(ModelAndView mav, Long parentCategoryId, Long categoryId, Integer pageNum, Integer pageSize) {
        mav.setViewName("frontend/goods_list");
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = WeGo.PAGE_SIZE;
        }

        //查找到需要显示的顶级类别
        List<CategoryVO> topCategoryVOList = categoryService.getTopCategory(WeGo.TOP_CATEGORY_AMOUNT);
        for (CategoryVO topCategoryVO : topCategoryVOList) {
            List<CategoryVO> secondCategoryVOList = categoryService.getCategoryByPid(topCategoryVO.getId(), 1, WeGo.SECOND_CATEGORY_AMOUNT);
            topCategoryVO.setSub(secondCategoryVOList);
        }
        mav.addObject("categoryList", topCategoryVOList);
        //所有子类别的ID
        List<Long> subIds = new ArrayList<>();
        if (parentCategoryId != null) {
            List<CategoryVO> subCategoryList = categoryService.getCategoryByPid(parentCategoryId, 1, Integer.MAX_VALUE);
            for (CategoryVO categoryVO : subCategoryList) {
                subIds.add(categoryVO.getId());
            }
            GoodsQuery goodsQuery = new GoodsQuery();
            goodsQuery.setPageNum(pageNum);
            goodsQuery.setPageSize(pageSize);
            goodsQuery.setState(1);
            goodsQuery.setCategoryIds(subIds);
            PageBean<GoodsVO> goodsVOPageBean = goodsService.getGoodsByConditionWithPage(goodsQuery);
            mav.addObject("goodsVOPageBean", goodsVOPageBean);

            mav.addObject("parentCategoryId", parentCategoryId);
        }

        if (categoryId != null) {
            GoodsQuery goodsQuery = new GoodsQuery();
            goodsQuery.setPageNum(pageNum);
            goodsQuery.setPageSize(pageSize);
            goodsQuery.setState(1);
            goodsQuery.setCategoryId(categoryId);
            PageBean<GoodsVO> goodsVOPageBean = goodsService.getGoodsByConditionWithPage(goodsQuery);
            mav.addObject("goodsVOPageBean", goodsVOPageBean);
            mav.addObject("categoryId", categoryId);
        }

        if(categoryId == null && parentCategoryId == null){
            GoodsQuery goodsQuery = new GoodsQuery();
            goodsQuery.setPageNum(pageNum);
            goodsQuery.setPageSize(pageSize);
            goodsQuery.setState(1);
            PageBean<GoodsVO> goodsVOPageBean = goodsService.getGoodsByConditionWithPage(goodsQuery);
            mav.addObject("goodsVOPageBean",goodsVOPageBean);
        }


        return mav;
    }

}
